<template>
	<el-form ref="loginForm" :model="form" :rules="rules" label-width="0" size="large" @keyup.enter="login">
		<el-form-item prop="phone">
			<el-input v-model="form.phone" prefix-icon="el-icon-iphone" clearable :placeholder="$t('login.mobilePlaceholder')">
				<template #prepend>+86</template>
			</el-input>
		</el-form-item>
		<el-form-item prop="yzm"  style="margin-bottom: 35px;">
			<div class="login-msg-yzm">
				<el-input v-model="form.yzm" prefix-icon="el-icon-unlock" clearable :placeholder="$t('login.smsPlaceholder')"></el-input>
				<el-button @click="getYzm" :disabled="disabled">{{this.$t('login.smsGet')}}<span v-if="disabled"> ({{time}})</span></el-button>
			</div>
		</el-form-item>
		<el-form-item>
			<el-button type="primary" style="width: 100%;" :loading="islogin" round @click="login">{{ $t('login.signIn') }}</el-button>
		</el-form-item>
		<!-- <div class="login-reg">
			{{$t('login.noAccount')}} <router-link to="/user_register">{{$t('login.createAccount')}}</router-link>
		</div> -->
	</el-form>
</template>

<script>
	import {filterAsyncRouter} from '@/utils/tools'
	export default {
		data() {
			return {
				form: {
					phone: "",
					yzm: "",
					autologin: true
				},
				rules: {
					phone: [
						{required: true, message: this.$t('login.mobileError')}
					],
					yzm: [
						{required: true, message: this.$t('login.smsError')}
					]
				},
				disabled: false,
				time: 0,
				islogin: false,
			}
		},
		mounted() {

		},
		methods: {
			async getYzm(){
				var validate = await this.$refs.loginForm.validateField("phone").catch(()=>{})
				if(!validate){ return false }

				//获取短信验证码
				var user = await this.$API.common.smsSend.get({phone: this.form.phone})
				if(user.code == 200){
					this.$message.success(this.$t('login.smsSent'))
					this.disabled = true
					this.time = 60
					var t = setInterval(() => {
						this.time -= 1
						if(this.time < 1){
							clearInterval(t)
							this.disabled = false
							this.time = 0
						}
					},1000)
				}else{
					this.$message.warning(user.msg)
					return false
				}

			},
			async login(){
				var validate = await this.$refs.loginForm.validate().catch(()=>{})
				if(!validate){ return false }
				//获取token
				var user = await this.$API.auth.memberLogin.post({phone: this.form.phone, password: this.form.yzm, type: 2})
				if(user.code == 200){
					this.$TOOL.cookie.set("TOKEN", user.data.token, {
						expires: this.form.autologin? 24*60*60 : 0
					})
					this.$TOOL.data.set("USER_INFO", {...user.data, dashboard: '0'})
				}else{
					this.islogin = false
					this.$message.warning(user.msg)
					return false
				}
				//获取菜单
				var menu = await this.$API.system.menu.menuRoleMenu.get()
				if(menu.code == 200){
					if(menu.data.menu.length==0){
						this.$alert("当前用户无任何菜单权限，请联系系统管理员", "无权限访问", {
							type: 'error',
							center: true
						})
						this.islogin = false
						return false
					}
					this.$TOOL.data.set("MENU", filterAsyncRouter(menu.data.menu))
					this.$TOOL.data.set("PERMISSIONS", menu.data.permissions || [])
					// this.$TOOL.data.set("MENU", menu.data.menu)
					// this.$TOOL.data.set("PERMISSIONS", menu.data.permissions)
					// this.$TOOL.data.set("DASHBOARDGRID", menu.data.dashboardGrid)
					// this.$TOOL.data.set("DASHBOARDGRID", ["welcome", "ver", "time", "progress", "echarts", "about"])
				}else{
					this.islogin = false
					this.$message.warning(menu.msg)
					return false
				}

				this.$router.replace({
					path: '/'
				})
				this.$message.success("Login Success 登录成功")
				this.islogin = false
			}
		}
	}
</script>

<style>

</style>
